package com.qlk.vorder.controller.monitor;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.qlk.common.utils.config.PropertiesHelper;
import com.qlk.common.utils.mail.SendMailUtil;
import com.qlk.vorder.domain.Task;
import com.qlk.vorder.domain.query.TaskQuery;
import com.qlk.vorder.service.TaskService;

/**
 * task异常数据监控 以邮件发出
 * 
 * @author js
 * 
 */
public class MonitorTask {
	private final Log logger = LogFactory.getLog(this.getClass());
	@Autowired
	private TaskService taskService;

	public void work() {
		logger.info(">>>vorderTask数据监控<<<");
		TaskQuery queryBean = new TaskQuery();
		String[] str = { "3", "4" };
		queryBean.setListStatus(Arrays.asList(str));
		List<Task> list = taskService.queryTaskList(queryBean);
		String strError = "";
		String sendMailAddress = "";// 收件人
		String formMailAddress = "";// 发件人
		if (CollectionUtils.isNotEmpty(list)) {
			for (Task task : list) {
				strError += task.getKeyword1() + "</br>";
			}
			StringBuffer context = new StringBuffer();
			context.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">")
					.append("<html>").append("<head>").append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">")
					.append("<title>创建退款单异常</title>").append("<style type=\"text/css\">")
					.append(".test{font-family:\"Microsoft Yahei\";font-size: 18px;color: red;}").append("</style>").append("</head>")
					.append("<body>").append("<span class=\"test\"><font size=\"3\" color=\"red\">创建退款单数据异常,订单号如下：</font></span>").append("</br>")
					.append("<span>" + strError + "</span>").append("</body>").append("</html>");
			// qlkmonitor testqlk smtp.163.com
			sendMailAddress = PropertiesHelper.newInstance().getValue("vorder.emil.sendMailAddress");
			formMailAddress = PropertiesHelper.newInstance().getValue("vorder.emil.formMailAddress");
			try {
				//SendMailUtils.sendMail("smtp.qq.com", "创建退款单异常", context.toString(), formMailAddress, sendMailAddress, cc);
				SendMailUtil.sendMail("true", "smtp", "smtp.exmail.qq.com", formMailAddress, "qlk123", "创建退款单异常", context.toString(), "text/html;charset=gbk", formMailAddress, sendMailAddress);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
